<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <span>支付方式管理</span>
      </div>
    </template>
    <el-row class="container">
      <el-col :span="2" class="text-center">支付方式</el-col>
      <el-col :span="22" class="item-container">
        <el-popconfirm
          v-for="item in paymentTypeList"
          :key="item.id"
          width="220"
          confirm-button-text="删除"
          cancel-button-text="修改"
          cancel-button-type="warning"
          @confirm="deleteHandle(item)"
          @cancel="editHandle(item)"
          :title="`修改或删除${item.name}(${item.additionalInfo})`"
        >
          <template #reference>
            <div class="item m-r-5 pointer">
              <el-tag style="padding: 13px">
                {{ `${item.name}(${item.additionalInfo})` }}
              </el-tag>
            </div>
          </template>
        </el-popconfirm>
        <el-icon class="item m-r-5 pointer" :size="35" color="#409EFF" @click="addHandle()">
          <CirclePlus />
        </el-icon>
      </el-col>
    </el-row>
    <!--支付方式信息修改弹窗管理-->
    <el-dialog v-model="dialog.visible" :destroy-on-close="true" :title="dialog.title" style="max-width: 700px">
      <el-form ref="paymentTypeFormRef" :model="form" :rules="rules" paymentType-width="120px">
        <el-form-item label="支付方式名称" prop="name">
          <el-input v-model.trim="form.name" autocomplete="off" placeholder="请输入，例：月付" />
        </el-form-item>
        <el-form-item label="每次支付月数" prop="payMonthCount">
          <el-input-number v-model.trim="form.payMonthCount" :min="1" class="!w-100%" step-strictly placeholder="请输入，例：3" />
        </el-form-item>
        <el-form-item label="支付方式说明" prop="additionalInfo">
          <el-input v-model.trim="form.additionalInfo" autocomplete="off" placeholder="请输入，例：押一付一" />
        </el-form-item>
        <el-form-item label="支付押金金额" prop="payDeposit">
          <el-input-number v-model.trim="form.payDeposit" :min="1" class="!w-100%" step-strictly placeholder="请输入押金金额" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>
<script setup lang="ts">
import { PaymentTypeVO, PaymentTypeForm, PaymentTypeQuery } from '@/api/apartment/attribute/paymentType/types';
import { listPaymentType, getPaymentType, addPaymentType, updatePaymentType, delPaymentType } from '@/api/apartment/attribute/paymentType/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const buttonLoading = ref(false);
const paymentTypeFormRef = ref<ElFormInstance>();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const initFormData: PaymentTypeForm = {
  id: undefined,
  name: undefined,
  payMonthCount: undefined,
  additionalInfo: undefined,
  payDeposit: undefined,
  sort: undefined
};
const data = reactive<PageData<PaymentTypeForm, PaymentTypeQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    payMonthCount: undefined,
    additionalInfo: undefined,
    payDeposit: undefined,
    sort: undefined,
    params: {}
  },
  rules: {
    name: [{ required: true, message: '支付方式名称', trigger: 'blur' }],
    payMonthCount: [{ required: true, message: '请输入每次支付月数', trigger: 'blur' }],
    additionalInfo: [{ required: true, message: '请输入支付方式说明', trigger: 'blur' }],
    payDeposit: [{ required: true, message: '请输入支付押金金额', trigger: 'blur' }]
  }
});
const { queryParams, form, rules } = toRefs(data);
// 支付方式类型
const paymentTypeList = ref<PaymentTypeVO[]>([]);
// 获取支付方式信息
const getPaymentTypeInfoListHandle = async () => {
  const { data } = await listPaymentType(queryParams.value);
  paymentTypeList.value = data;
};
// 删除支付方式
const deleteHandle = async (item: PaymentTypeVO) => {
  const _ids = item?.id;
  await proxy?.$modal.confirm('是否确认删除支付方式信息编号为"' + _ids + '"的数据项？');
  await delPaymentType(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getPaymentTypeInfoListHandle();
};
// 编辑支付方式
const editHandle = async (item: PaymentTypeVO) => {
  reset();
  const _id = item?.id;
  const res = await getPaymentType(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改支付方式信息';
};
// 添加支付方式
const addHandle = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加支付方式信息';
};
/** 提交按钮 */
const submitForm = () => {
  paymentTypeFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updatePaymentType(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addPaymentType(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getPaymentTypeInfoListHandle();
    }
  });
};
/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  paymentTypeFormRef.value?.resetFields();
};
onMounted(() => {
  getPaymentTypeInfoListHandle();
});
</script>

<style scoped lang="scss">
.card-header {
  font-size: 18px;
  font-weight: bold;
}

.text-center {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  text-align: center;
}

.container:not(:first-child) {
  margin-top: 20px;
}

.item-container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  padding: 10px 15px;
  background-color: #efefef;
  border-radius: 20px;

  .item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
}
</style>
